﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ParkingLot.Utility
{
    /// <summary>
    ///文件转Byte的帮助类
    /// </summary>
    public class FileByteHelp
    {
        /// <summary>
        /// 将文件转换为Byte流
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="outBytes"></param>
        /// <param name="error"></param>
        public static void GetBytes(string filePath,out byte[] outBytes, out string error)
        {
            outBytes = null;
            error = string.Empty;

            //能否执行判断
            FileInfo fileInfo = new FileInfo(filePath);
            if (fileInfo == null)
            {
                error = "文件不存在";
                return;
            }
            FileStream fileStream = fileInfo.OpenRead();
            if (fileStream == null)
            {
                error = "文件读取失败";
                return;
            }

            //执行转换
            try
            {
                outBytes = new byte[fileStream.Length];
                fileStream.Read(outBytes, 0, outBytes.Length);
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }
            finally
            {
                //释放资源
                fileStream.Close();
            }
        }

        public static void GetBytes(FileInfo fileInfo, out byte[] outBytes, out string error)
        {
            outBytes = null;
            error = string.Empty;

            //能否执行判断
            if (fileInfo == null)
            {
                error = "文件不存在";
                return;
            }
            FileStream fileStream = fileInfo.OpenRead();
            if (fileStream == null)
            {
                error = "文件读取失败";
                return;
            }

            //执行转换
            try
            {
                outBytes = new byte[fileStream.Length];
                fileStream.Read(outBytes, 0, outBytes.Length);
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }
            finally
            {
                //释放资源
                fileStream.Close();
            }
        }

        /// <summary>
        /// 保存文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="bytes"></param>
        public static void Save(string filePath, byte[] bytes)
        {
            
        }

    }
}
